using System;
using UnityEngine;
using QFramework;

namespace Survivor
{
	public partial class Knife : ViewController
	{
		[HideInInspector] public Vector2 direciton;
		[HideInInspector] public float speed;
		public float aliveTime;
		void Start()
		{
			ActionKit.Delay(aliveTime, () =>
			{
				gameObject.DestroySelfGracefully();
			}).Start(this);
		}

		public void Init(Vector2 position,Vector2 dir,float sp)
		{
			transform.position = position;
			direciton = dir.normalized;
			speed = sp;
			var angle = -Mathf.Atan2(dir.x, dir.y)*Mathf.Rad2Deg;
			transform.rotation = Quaternion.Euler(0, 0, angle);
		}

		private void Update()
		{
			transform.position +=
				new Vector3(direciton.x * speed * Time.deltaTime, direciton.y * speed * Time.deltaTime);
		}

		private void OnTriggerEnter2D(Collider2D other)
		{
			var enemy = other.GetComponentInParent<Enemy>();
			if (enemy)
			{
				enemy.Hurt(Global.SimpleKnifeDamage.Value);
				gameObject.DestroySelfGracefully();
			}
		}
	}
}
